package com.mate.cloud.codegen.controller;


import cn.hutool.core.io.IoUtil;
import cn.hutool.http.ContentType;
import cn.smallbun.screw.boot.config.Screw;
import cn.smallbun.screw.boot.properties.ScrewProperties;
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mate.cloud.codegen.croe.R;
import com.mate.cloud.codegen.domain.GenDatasourceConf;
import com.mate.cloud.codegen.service.GenDatasourceConfService;
import com.mate.cloud.codegen.util.SpringContextHolder;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import javax.sql.DataSource;

/**
 * 数据源管理
 *
 * @author: MI
 * @email: 448341911@qq.com
 * @createDate: 2023/11/5 21:29
 * @updateUser: MI
 * @updateDate: 2023/11/5 21:29
 * @updateRemark: 修改内容
 * @version: v1.0
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/dsconf")
public class GenDsConfController {

    private final GenDatasourceConfService datasourceConfService;

    private final Screw screw;

    /**
     * 分页查询
     *
     * @param page           分页对象
     * @param datasourceConf 数据源
     * @return
     */
    @GetMapping("/page")
    public R GenDatasourceConfPage(Page page, GenDatasourceConf datasourceConf) {
        return R.ok(datasourceConfService.page(page, Wrappers.query(datasourceConf)));
    }

    /**
     * 查询全部数据源
     *
     * @return
     */
    @GetMapping("/list")
    public R list() {
        return R.ok(datasourceConfService.list());
    }

    /**
     * 通过id查询数据源表
     *
     * @param id id
     * @return R
     */
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Integer id) {
        return R.ok(datasourceConfService.getById(id));
    }

    /**
     * 新增数据源表
     *
     * @param datasourceConf 数据源表
     * @return R
     */
    @PostMapping
    public R save(@RequestBody GenDatasourceConf datasourceConf) {
        return R.ok(datasourceConfService.saveDsByEnc(datasourceConf));
    }

    /**
     * 修改数据源表
     *
     * @param conf 数据源表
     * @return R
     */
    @PutMapping("/edit")
    public R updateById(@RequestBody GenDatasourceConf conf) {
        return R.ok(datasourceConfService.updateDsByEnc(conf));
    }

    /**
     * 通过id删除数据源表
     *
     * @param id id
     * @return R
     */
    @DeleteMapping("/{id}")
    public R removeById(@PathVariable Integer id) {
        return R.ok(datasourceConfService.removeByDsId(id));
    }


    /**
     * 查询数据源对应的文档
     * @param dsName 数据源名称
     */
    @SneakyThrows
    @GetMapping("/doc")
    public void generatorDoc(String dsName, HttpServletResponse response) {
        // 设置指定的数据源
        DynamicRoutingDataSource dynamicRoutingDataSource = SpringContextHolder.getBean(DynamicRoutingDataSource.class);
        DynamicDataSourceContextHolder.push(dsName);
        DataSource dataSource = dynamicRoutingDataSource.determineDataSource();

        // 设置指定的目标表
        ScrewProperties screwProperties = SpringContextHolder.getBean(ScrewProperties.class);

        // 生成
        byte[] data = screw.documentGeneration(dataSource, screwProperties).toByteArray();
        response.reset();
        response.addHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(data.length));
        response.setContentType(ContentType.OCTET_STREAM.getValue());
        IoUtil.write(response.getOutputStream(), Boolean.TRUE, data);
    }
}
